package com.nativetalk.controller.app.rongCloud;

import com.aliyun.openservices.ClientException;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.rongyun.RongCloudConfig;
import com.nativetalk.bean.rongyun.TokenResult;
import com.nativetalk.bean.rongyun.UserCommit;
import com.nativetalk.common.rongCloud.RongCloud;
import com.nativetalk.service.rongCloud.RongCloudServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.Reader;

@Controller
public class RongCloudController {

    Logger logger = LoggerFactory.getLogger(RongCloudController.class);

    @Resource
    private RongCloudConfig rongCloudConfig;

    @Resource
    private RongCloudServiceImpl rongCloudService;

    /**
     * 重新获取老师和学生的融云token
     * @return
     */
    @RequestMapping(value = "/rongyun/users/getToken", method = {RequestMethod.POST})
    @ResponseBody
    public RetInfo getStudentToken(){
        RetInfo retInfo = new RetInfo();
        retInfo = rongCloudService.getUsersToken();
        return retInfo;
    }

    @RequestMapping(value="/rongyun/getUserMessageById")
    @ResponseBody
    public RetInfo getUserMessageById(@RequestBody UserCommit userCommit){
        RetInfo retInfo = new RetInfo();
        try{
            if(userCommit.getUserId() == null || userCommit.getUserId() == ""){
                retInfo.setMark(RetInfo.ReturnCode.CLIENT_ERROR);
                retInfo.setTip(RetInfo.ReturnCode.CLIENT_ERROR_MSG_CN);
                return retInfo;
            }
            UserCommit obj=rongCloudService.getUserById(userCommit.getUserId());
            retInfo.setMark(RetInfo.ReturnCode.SUCCESS);
            retInfo.setTip(RetInfo.ReturnCode.SUCCESS_MSG_CN);
            retInfo.setObj(obj);
        }catch(ClientException e){
            logger.error(e.getMessage(),e);
            retInfo.setMark(RetInfo.ReturnCode.CLIENT_ERROR);
            retInfo.setTip(e.getMessage());
            return  retInfo;
        }
        return  retInfo;
    }
}
